import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test_demo/three/request/api_response.dart';
import 'package:flutter_test_demo/three/request/http_utils.dart';

class HttpPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HttpState();

}

class _HttpState extends State<HttpPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('网络请求'),
      ),
      body: Column(
        children: [

          ListTile(
            title: Text("相同baseUrl"),
            onTap: (){
              _commonHttp();
            },
          ),

          ListTile(
            title: Text("完整地址"),
            onTap: (){
              _otherHttp();
            },
          ),

        ],
      ),
    );
  }


  /// 相同baseUrl
  _commonHttp() async {
    HttpUtils.init(baseUrl: "https://wanandroid.com/");
    try {
      dynamic response = await HttpUtils.get("wxarticle/chapters/json");
      print(response);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      print(e);
      return ApiResponse.error(e.error);
    }

  }

  /// 不同baseUrl
  _otherHttp() async {
    try {
      dynamic response = await HttpUtils.get("https://wanandroid.com/article/listproject/0/json",);
      print(response);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      print(e);
      return ApiResponse.error(e.error);
    }

  }

}